package com.standardoperationapp.materialinventory.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.standardoperationapp.R;
import com.standardoperationapp.bean.MatPlanBean;
import com.standardoperationapp.databinding.PlanListItemBinding;
import com.standardoperationapp.global.ListViewItem;

import java.util.List;

public class MatPlanListAdapter extends BaseAdapter {

    private List<MatPlanBean.DataDTO.ListDTO> list;
    private LayoutInflater mInflater;

    public MatPlanListAdapter(Context context, List<MatPlanBean.DataDTO.ListDTO> list1) {
        list = list1;
        mInflater = LayoutInflater.from(context);
    }


    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public int getItemViewType(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        MatPlanBean.DataDTO.ListDTO item = list.get(position);
        PlanListItemBinding binding;
        if (convertView==null){
            binding= DataBindingUtil.inflate(mInflater,R.layout.plan_list_item,parent,false);
        }else {
            binding=DataBindingUtil.getBinding(convertView);
        }

//        MatPlanListAdapter.ViewHolder viewHolder = null;
//        PlanListItemBinding binding;
//        if (null == convertView) {
//            viewHolder = new ViewHolder();
//            binding = DataBindingUtil.inflate(mInflater, R.layout.plan_list_item, parent, false);
//            convertView = binding.getRoot();//获取布局
//            viewHolder.setItemCoinsDetailBinding(binding);//缓存binding到holder
//            convertView.setTag(viewHolder);//设置Tag
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//            binding = (PlanListItemBinding) viewHolder.getItemCoinsDetailBinding();
//        }

//        if (convertView == null) {
//            convertView = mInflater.inflate(R.layout.plan_list_item, null);
//            viewHolder = new PlanListAdapter.ViewHolder(
//                    (View) convertView.findViewById(R.id.list_child),
//                    (TextView) convertView.findViewById(R.id.notxt),
//                    (TextView) convertView.findViewById(R.id.positiontxt),
//                    (TextView) convertView.findViewById(R.id.nametxt),
//                    (TextView) convertView.findViewById(R.id.codetxt),
//                    (TextView) convertView.findViewById(R.id.numbertxt),
//                    (TextView) convertView.findViewById(R.id.scannumbertxt),
//                    (TextView) convertView.findViewById(R.id.infotxt)
//            );
//            convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (PlanListAdapter.ViewHolder) convertView.getTag();
//        }
//
//        viewHolder.noTxt.setText(Integer.toString(item.getNo()));
//        viewHolder.positionTxt.setText(item.getPosition());
//        viewHolder.nameTxt.setText(item.getName());
//        viewHolder.codeTxt.setText(item.getCode());
//        viewHolder.numberTxt.setText(Integer.toString(item.getNumber()));
//        viewHolder.scannoTxt.setText(Integer.toString(item.getScanno()));
//        viewHolder.infoTxt.setText(item.getInfo());
        if ((position % 2) == 1) {
            binding.listChild.setBackgroundColor(Color.parseColor("#F1F1F1"));
        } else {
            binding.listChild.setBackgroundColor(Color.WHITE);
        }
        //绑定数据
        binding.setData(item);

        return binding.getRoot();
    }

    private static class ViewHolder {
        private ViewDataBinding itemCoinsDetailBinding;

        public void setItemCoinsDetailBinding(ViewDataBinding itemCoinsDetailBinding) {
            this.itemCoinsDetailBinding = itemCoinsDetailBinding;
        }

        public ViewDataBinding getItemCoinsDetailBinding() {
            return itemCoinsDetailBinding;
        }
    }
}
